package
{
	import com.adobe.serialization.json.*;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.net.URLLoader;
	
	import org.flixel.*;

	public class FriendListing extends FlxGroup
	{
		public static const HEIGHT:Number = 120;
		public static const WIDTH:Number = 80;
		public static const BLANK:String = "-53257382";
		
		private var bg:FlxSprite;
		private var image:FlxSprite;
		private var imageFrame:FlxSprite;
		private var nameBar:FlxSprite;
		private var nameText:FlxText;
		private var overlapped:Boolean;
		
		private var name:String;
		private var id:String;
		
		private var startX:Number;
		private var startY:Number;
		
		private var fullyLoaded:Boolean;
		
		public function FriendListing(id:String, x:Number, y:Number)
		{
			this.id = id;
			name = "";
			
			startX = x;
			startY = y;
			
			if (id == BLANK) {
				drawPlaceholder();
				return;
			}
			
			// Send a request for the bitmap data for friend's photo.
			Elemental.fbInfo.addPicture(id, function f(e:Event):void {
				var img:Bitmap = new Bitmap(e.target.content.bitmapData); 
				image = new FlxSprite();
				image.pixels = img.bitmapData;
				
				image.x = bg.x + (bg.width-image.width) / 2;
				image.y = bg.y + 10;
				add(image);
				
				if (name != "" && image != null) {
					fullyLoaded = true;
				}
			}); 
			
			// Send a request for the first name.
			Elemental.fbInfo.getFriendInfo(id, function f(e:Event):void {
				var urlLoader:URLLoader = URLLoader(e.target);
				var serverReply:String = urlLoader.data;
				
				if (serverReply.length > 0) {
					var obj:Object = JSON.decode(serverReply);
					name = obj["first_name"];
					nameText = new FlxText(nameBar.x, 0, nameBar.width, name, false);
					nameText.setFormat("Courier New", 14, 0x000000, "center", 0, true);
					nameText.y = nameBar.y + (nameBar.height - nameText.height)/2;
					add(nameText);
					
					if (name != "" && image != null) {
						fullyLoaded = true;
					}
				}
			});
			
			drawListing();
		}
		
		/**
		 * While we're waiting for the name and image to load
		 * Might as well draw the rest of the listing
		 */
		public function drawListing():void {
			// background container
			bg = new FlxSprite(startX, startY, Res.FriendListingBG);
			add(bg);
			
			// the stripe that holds the name text
			nameBar = new FlxSprite(bg.x, bg.y + 10 + 50 + 15);
			nameBar.makeGraphic(WIDTH, 30, 0x000000, true);
			nameBar.drawRect(0, 0, WIDTH, HEIGHT, 0xFFEE5F);
			
			overlapped = false;
		}
		
		public function drawPlaceholder():void {
			bg = new FlxSprite(startX, startY);
			bg.makeGraphic(WIDTH, HEIGHT, 0x000000, true);
			bg.drawRect(0, 0, WIDTH, HEIGHT, 0x8F4400);
			add(bg);
			fullyLoaded = true;
		}
		
		public override function update():void {
			super.update();
			
			if (bg == null || !fullyLoaded || id == BLANK) return;
			
			if (bg.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
				if (!overlapped) {
					bg.loadGraphic(Res.FriendListingHoverBG);
					overlapped = true;
				}
			} else {
				if (overlapped) {
					bg.loadGraphic(Res.FriendListingBG);
					overlapped = false;
				}
			}
			
			if (FlxG.mouse.justPressed() && overlapped) {
				FlxG.switchState(new FriendRoomState(id, name));
			}
		}
		
		public function isFullyLoaded():Boolean {
			return fullyLoaded;
		}
	}
}